package com.cxmx.escort.base;

import com.cxmx.escort.common.redis.RedisUtils;

import java.security.MessageDigest;
import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * import java.security.MessageDigest;
 * <p>
 * /**
 *
 * @Author:Starry
 * @Description:
 * @Date:Created in 9:46 2018/4/13
 * Modified By:
 */
public class MD5Utils {


    private static final String hexDigIts[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"};

    /**
     * MD5加密
     *
     * @param origin      字符
     * @param charsetname 编码
     * @return
     */
    public static String MD5Encode(String origin, String charsetname) {
        String resultString = null;
        try {
            resultString = new String(origin);
            MessageDigest md = MessageDigest.getInstance("MD5");
            if (null == charsetname || "".equals(charsetname)) {
                resultString = byteArrayToHexString(md.digest(resultString.getBytes()));
            } else {
                resultString = byteArrayToHexString(md.digest(resultString.getBytes(charsetname)));
            }
        } catch (Exception e) {
        }
        return resultString;
    }


    public static String byteArrayToHexString(byte b[]) {
        StringBuffer resultSb = new StringBuffer();
        for (int i = 0; i < b.length; i++) {
            resultSb.append(byteToHexString(b[i]));
        }
        return resultSb.toString();
    }

    public static String byteToHexString(byte b) {
        int n = b;
        if (n < 0) {
            n += 256;
        }
        int d1 = n / 16;
        int d2 = n % 16;
        return hexDigIts[d1] + hexDigIts[d2];
    }

    public static double getPrice(LocalDateTime startTime, LocalDateTime endTime, double unitPrice, String userId) {
        String times = String.valueOf(RedisUtils.get(LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYYMMdd")) + ":" + userId));
        int time = Integer.parseInt(times.equals("null") ? "0" : times);
        Duration a = Duration.between(startTime, endTime);
        boolean halfFlag = false;
        int beginHour = startTime.getHour();
        //如果开始时间小于半小时
        if (startTime.getMinute() < 30) {
            beginHour = beginHour - 1;
        }

        //如果不满30分钟
        if (a.toMinutes() < 30) {
            if (time > 1) {
                return unitPrice;
            } else {
                return 0;
            }
        }

        int endHour = endTime.getHour();
        int minutes = (int) a.toMinutes() % 60;
        int hours = (int) a.toMinutes() / 60;
        double price = 0;
        int differHour = (int) a.toDays();
        //超过一天
        if (differHour > 0) {
            //开始时间大于下午2点
            if (beginHour <= 14) {
                price += unitPrice * 10;
            } else {
                price += (24 - beginHour) * unitPrice;
            }

            //开始时间大于下午2点
            if (endHour >= 14) {
                halfFlag = true;
                price += unitPrice * 10;
            } else {
                price += endHour * unitPrice;
            }

            //算出计费
            if (differHour - 1 > 0) {
                price += (differHour - 1) * 20;
            }
        } else {
            if (hours < 10) {
                price += hours * unitPrice;
            } else {
                halfFlag = true;
                price += unitPrice * 10;
            }
        }

        //多出时间
        if (minutes >= 30 && !halfFlag) {
            price = price + unitPrice;
        }
        return price;
    }

}
